/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.inbound.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.basic.warehouse.mapper.WmsBasicWarehouseMapper;
import org.jeecg.modules.business.strategy.InReceiptStrategy;
import org.jeecg.modules.business.strategy.entity.InReceiptStrategyParam;
import org.jeecg.modules.business.strategy.entity.InReceiptStrategyWarehouse;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 入库任务提交之后调用入库策略（不传给MES,只用于保存数据库）
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-09-26
 */
@Component
public class WmsInReceiptTaskStrategy<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Resource
    private WmsBasicWarehouseMapper warehouseMapper;
    @Autowired
    private IWmsSapWarehouseMapService iWmsSapWarehouseMapService;
    @Autowired
    private InReceiptStrategy inReceiptStrategy;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        String taskId = baseFlowDto.getInfoId();
        WmsInReceiptTask task = iWmsInReceiptTaskService.getById(taskId);
        List<WmsInReceiptTaskMaterial> list = iWmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId)
        );
        if (CollectionUtil.isNotEmpty(list)){
            List<InReceiptStrategyParam> params = new ArrayList<>();
            for (WmsInReceiptTaskMaterial o : list) {
                InReceiptStrategyParam param = new InReceiptStrategyParam();
                param.setId(o.getId());
                param.setWarehouseId(o.getWmsBasicWarehouseId());
                param.setMaterialId(o.getWmsBasicMaterialId());
                param.setProductSite(o.getPlanProductSite());
                param.setPlanProductOrderNumber(task.getProductOrderNumber());
                param.setShopSign(o.getShopSign());
                param.setWidth(o.getWidth());
                param.setUnwarehousedQuantity(o.getUnwarehousedQuantity());
                param.setUnwarehousedQuantityAssist(o.getUnwarehousedQuantityAssist());
                param.setLocation(o.getLocation());
                param.setDocumentType(task.getDocumentType());
                param.setOriginalSystem(task.getOriginalSystem());
                param.setBatchCode(o.getBatchCode());
                param.setProductCode(o.getProductNumber());
                param.setSite(task.getSiteCode());
                param.setFactory(o.getFactory());
                param.setSapLocation(o.getSapLocation());
                param.setTaskId(taskId);
                param.setPlanQuantity(o.getPlanQuantity());
                params.add(param);
                //如果sapLocationFactory有值的话,就不调用入库策略,没有值才调用
                if (StrUtil.isEmpty(o.getSapLocationFactory())){
//                    InReceiptStrategyWarehouse strategyWarehouse = inReceiptStrategy.strategyLogicInvoke(param,task.getDocumentType());
//                    if (ObjectUtil.isNotNull(strategyWarehouse)){
//                        o.setWmsBasicWarehouseId(strategyWarehouse.getWarehouseId());
//                        o.setWmsBasicWarehouseAreaId(StrUtil.isNotBlank(strategyWarehouse.getWarehouseAreaId())?strategyWarehouse.getWarehouseAreaId():"" );
//                        o.setWmsBasicWarehouseAreaLocationId(StrUtil.isNotBlank(strategyWarehouse.getWarehouseAreaLocationId())?strategyWarehouse.getWarehouseAreaLocationId():"");
//                        iWmsInReceiptTaskMaterialService.updateById(o);
//                    }
                    List<InReceiptStrategyWarehouse> strategyList = inReceiptStrategy.strategyLogicInvoke(params, task.getDocumentType());
                    if (CollectionUtil.isNotEmpty(strategyList)) {
                        List<WmsInReceiptTaskMaterial> materials = new ArrayList<>();
                        for (InReceiptStrategyWarehouse strategyWarehouse : strategyList) {
                            if (ObjectUtil.isNotNull(strategyWarehouse)) {
                                WmsInReceiptTaskMaterial material = new WmsInReceiptTaskMaterial();
                                material.setId(strategyWarehouse.getId());
                                material.setWmsBasicWarehouseId(strategyWarehouse.getWarehouseId());
                                material.setWmsBasicWarehouseAreaId(StrUtil.isNotBlank(strategyWarehouse.getWarehouseAreaId()) ? strategyWarehouse.getWarehouseAreaId() : "");
                                material.setWmsBasicWarehouseAreaLocationId(StrUtil.isNotBlank(strategyWarehouse.getWarehouseAreaLocationId()) ? strategyWarehouse.getWarehouseAreaLocationId() : "");
                                materials.add(material);
                            }
                        }
                        iWmsInReceiptTaskMaterialService.updateBatchById(materials);
                    }
                }
            }
        }
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setResult(Result.OK());
    }
}
